package pl.ptemich.nov2k13.states;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import pl.ptemich.nov2k13.engine.HexBoard;
import pl.ptemich.nov2k13.engine.HexBoard.HexType;
import pl.ptemich.nov2k13.engine.HexBoard.MouseButton;
import pl.ptemich.tankwars.tools.LoggingTools;

public class TacticalGameState extends BasicGameState {

  private HexBoard board;
  
  int mouseX = 0;
  int mouseY = 0;
  
  @Override
  public int getID() {   
    return 666;
  }
  
  @Override
  public void init(GameContainer container, StateBasedGame game)
      throws SlickException {
    LoggingTools.debug("init hex board");
    // TODO Currently ONLY LFAT orientation is suppored 
    board = new HexBoard(100, 100, HexType.FLAT_ORIENTATION); 
  }
  
  @Override
  public void render(GameContainer container, StateBasedGame game, Graphics graphics)
      throws SlickException {        
    board.draw(graphics);
    graphics.drawString(mouseX + " - " + mouseY, 20, 20); 
  }
  
  @Override
  public void update(GameContainer container, StateBasedGame game, int delta)
      throws SlickException {

    Input input = container.getInput();
    if (input.isKeyDown(Input.KEY_ESCAPE)) {
      container.exit();
    }    
    
    if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
      board.mouseClick(input.getMouseX(), input.getMouseY(), MouseButton.LEFT);
    } else if (input.isMousePressed(Input.MOUSE_RIGHT_BUTTON)) {
      board.mouseClick(input.getMouseX(), input.getMouseY(), MouseButton.RIGHT);
    } else if (input.isMousePressed(Input.MOUSE_MIDDLE_BUTTON)) {
      board.mouseClick(input.getMouseX(), input.getMouseY(), MouseButton.MIDDLE);
    }
  }
  
  @Override
  public void mouseMoved(int oldx, int oldy, int newx, int newy) {
    mouseX = newx;
    mouseY = newy; 
    board.mouseMove(newx, newy);
  }
  
//  @Override
//  public void keyPressed(int key, char c) {    
//    super.keyPressed(key, c);
//      switch (key) {
//        case Input.KEY_ESCAPE:
//          container.exit();
//          break;
//      }
//  }  
}
